#ifndef _MANAGEMENT_H_
#define _MANAGEMENT_H_

/***** standard *****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/******** IO ********/
#include <pthread.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>

/******* net ********/
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

/*
 *   define
 */
#define YW_NUMFORARR 64

#define YW_MSG_M0_FRESH     1
#define YW_MSG_M0_ENV       2
#define YW_MSG_HTML_ORDER   3
#define YW_MSG_HTML_LOGIN   4
#define YW_MSG_HTML_POST    5
#define YW_MSG_SQL_DATA     6

#define YW_ORDER_FAN_CLOSE  0x40
#define YW_ORDER_FAN_ONE    0x41
#define YW_ORDER_FAN_TWO    0x42
#define YW_ORDER_FAN_THREE  0x43

#define YW_ORDER_BEEP_CLOSE 0x50
#define YW_ORDER_BEEP_OPEN  0x51
#define YW_ORDER_BEEP_AUTOC 0x52
#define YW_ORDER_BEEP_AUTOO 0x53

#define YW_ORDER_LED_CLOSE  0x60
#define YW_ORDER_LED_OPEN   0x61

#define YW_ORDER_DIG_CLOSE  0x7f
#define YW_ORDER_DIG_NUM[10]  {0x70, 0x71, 0x72, 0x73, 0x74,\
	                           0x75, 0x76, 0x77, 0x78, 0x79}

/*
 *   struct
 */
typedef struct{
	float temperature;
	float humidity;
	float illumination;
}YW_ENV;


typedef struct{
	float temperature_max;
	float temperature_min;
	float humidity_max;
	float humidity_min;
	float illumination_max;
	float illumination_min;
}YW_ENV_SET;

typedef struct{
	int  id;
	char name[YW_NUMFORARR];
	char pwd[YW_NUMFORARR];
}YW_USER;

typedef struct{
	long type;
	int  order_type;
	int  order_content;
}YW_ORDER;

typedef struct{
	int   id;
	char  name[YW_NUMFORARR];
	char  time[YW_NUMFORARR];
	int   count;
	float price;
}YW_GOODS;

typedef struct{
	long msg_type;
    long type;
	unsigned char text[YW_NUMFORARR];
}YW_MSG;

typedef struct{
	uint8_t  head[3];
	uint8_t  type;
	uint8_t  snum;
	uint8_t  temp[2];
	uint8_t  hum[2];
	uint8_t  x;
	uint8_t  y;
	uint8_t  z;
	uint32_t ill;
	uint32_t bet;
	uint32_t adc;
}YW_SENDENV;

typedef struct{
	uint8_t  head[3];
	uint8_t  type;
	uint8_t  snum;
	uint8_t  io;
	uint8_t  goodsno;
	uint8_t  goodsnum;
}YW_SENDGOODS;

//send_env -> shm -> cgi
typedef struct{
	unsigned char goods_type;
	unsigned int  goods_count;
}YW_STORAGE_GOODS_INFO;

typedef struct{
	unsigned char storage_status;
	unsigned char led_status;
	unsigned char buzzer_status;
	unsigned char fan_status;
	unsigned char seg_status;
	signed   char x;
	signed   char y;
	signed   char z;
	char   samplingTime[20];
	float  temp;
	float  temp_min;
	float  temp_max;
	float  hum;
	float  hum_min;
	float  hum_max;
	float  ill;
	float  ill_min;
	float  ill_max;
	float  battery;
	float  adc;
	float  adc_min;
	YW_STORAGE_GOODS_INFO goods_info[YW_NUMFORARR];
}YW_STORAGE_INFO;

typedef struct{
	YW_STORAGE_INFO storage_no[YW_NUMFORARR];
}YW_ENV_INFO_CLIEN_ADDR;

//used in shm
typedef struct{
	char cgi_status;
	char qt_status;
	YW_ENV_INFO_CLIEN_ADDR rt_status;
}YW_SHM_ADDR;

pthread_mutex_t mutex_sql;
pthread_mutex_t mutex_zig;
pthread_mutex_t mutex_msg;

/*
 *   function --- m0
 */

void* yw_thread_m0(void *arg)   //wait and accept data from m0.

void yw_read_zigbee(); //wait for zigbee data and analysis.

void yw_send_order();  //send order to m0.


/*
 *   function --- html
 */

void yw_thread_html();    //wait and accept data from html.

void yw_html_analysis();  //analysis html data.


/*
 *   function --- sqlite
 */

void yw_thread_sqlite();  //create it when you need to use sqlite.

void yw_get_env();        //get enviroment data from m0.

void yw_set_env();        //set enviroment data in table yw_t_set_env.

void yw_compare_env();    //compare data from m0 and data in table yw_t_set_env.

void yw_save_env();       //save enviroment data in time.

void yw_send_env();       //send enviroment data to html.



#endif
